package com.cmz.common.dto;

import com.alibaba.fastjson.JSON;
import com.cmz.common.api.dto.StandardResponse;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

/**
 * 通用响应
 * @author cmz
 * @param <T> 响应数据类型
 */
@Getter
@Setter
@ApiModel("通用响应")
public class Response<T> implements StandardResponse<T> {

    /** 响应标记 */
    @ApiModelProperty(name = "flag", value = "响应标记")
    private int flag;

    /** 响应数据 */
    @ApiModelProperty(name = "data", value = "响应数据")
    private T data;

    /** 响应消息 */
    @ApiModelProperty(name = "msg", value = "响应消息")
    private String msg;

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public static <T> Response<T> newInstance(T data) {
        Response<T> response = new Response<>();
        response.setData(data);
        response.setFlag(0);
        response.setMsg(null);
        return response;
    }

    public static Response<Void> newInstance() {
        Response<Void> response = new Response<>();
        response.setFlag(0);
        response.setMsg(null);
        return response;
    }

    public static Response<Void> newErrorInstance() {
        Response<Void> response = new Response<>();
        response.setFlag(-1);
        response.setMsg(null);
        return response;
    }

    public static Response<Void> newErrorInstance(String msg) {
        Response<Void> response = new Response<>();
        response.setFlag(-1);
        response.setMsg(msg);
        return response;
    }

    public static <T> Response<T> newErrorInstance(T data,String msg) {
        Response<T> response = new Response<>();
        response.setData(data);
        response.setFlag(-1);
        response.setMsg(null);
        return response;
    }

}
